package com.google.coffeeshop.gui.desktop.receiptnote;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JScrollPane;

import com.google.coffeeshop.business.AvailabilityBusiness;
import com.google.coffeeshop.entity.ReceiptNoteDTO;
import com.google.coffeeshop.gui.desktop.ContentBasePanel;

public class ReceiptNotePanel extends ContentBasePanel {
	private static final long serialVersionUID = 1L;
	
	private ReceiptNoteTable tblReceiptNote;
	
	public ReceiptNotePanel(String constraints) {
		super(constraints);
		setLayout(new BorderLayout(0, 0));
		
		initializeGUI();
	}

	private void initializeGUI() {
		tblReceiptNote = new ReceiptNoteTable();
		tblReceiptNote.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
					showAddEditEmployeeFrame();
				}
			}
		});
		add(new JScrollPane(tblReceiptNote), BorderLayout.CENTER);
		putAllReceiptNoteToTable(tblReceiptNote);		
	}
	
	public void showAddEditEmployeeFrame() {
		ReceiptNoteDTO receiptNote = tblReceiptNote.getReceiptNoteSelected();
		showAddEditEmployeeFrame(receiptNote);
	}
	
	private void showAddEditEmployeeFrame(ReceiptNoteDTO receiptNote) {
		AddViewReceiptNoteFrame frmAddViewReceiptNote = new AddViewReceiptNoteFrame(this, receiptNote);
		frmAddViewReceiptNote.setVisible(Boolean.TRUE);
	}
	
	public void refreshGUI() {
		removeAll();
		initializeGUI();
		validate();
		repaint();
	}
	
	private void putAllReceiptNoteToTable(ReceiptNoteTable receiptNoteTable) {
		receiptNoteTable.addNewRows(AvailabilityBusiness.findAllReceiptNotes());
	}
	
}
